﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CDStore3Tiers;
using DevComponents.DotNetBar;
using System.IO;
using log4net;
using log4net.Config;

namespace CuaHangDiaNhac
{
    public partial class frmLog : DevComponents.DotNetBar.Office2007Form
    {
        log4net.ILog a = log4net.LogManager.GetLogger("Log");
        StoreCDDataContext db;
        XuLyLog XLL = new XuLyLog();
        XuLyForm XL = new XuLyForm();
        private List<Log> _log;
        public frmLog()
        {
            InitializeComponent();
            db = new StoreCDDataContext();
            _log = XLL.list();
        }
        DataGridViewTextBoxColumn coID;
        DataGridViewTextBoxColumn coDate;
        DataGridViewTextBoxColumn coThread;
        DataGridViewTextBoxColumn coLevel;
        DataGridViewTextBoxColumn coLogger;
        DataGridViewTextBoxColumn coMessage;
        int PagingNumber = 0;
        int PagingSize = 25;
        public void LoadDS()
        {
            dgv.Refresh();
            db = new StoreCDDataContext();
            var log = db.Log_SelectAll();
            BindingSource l = new BindingSource();
            l.DataSource = log.Skip(PagingNumber).Take(PagingSize);
            bindingNavigator1.BindingSource = l;
            dgv.DataSource = l;
            lbPageTotal.Text = (XLL.TongSoTrang(PagingSize)).ToString();
            txtPage.Text = (XLL.SoTrangHienTai(PagingNumber, PagingSize)).ToString();
        }

        private void frmLog_Load(object sender, EventArgs e)
        {
            loadDGV();
        }
        public void loadDGV()
        {

            dgv.DataSource = null;

            coID = new DataGridViewTextBoxColumn();
            coID.DataPropertyName = "ID";
            coID.HeaderText = "Mã Log";
            coID.Name = "cnameID";
            coID.Visible = false;
            dgv.Columns.Add(coID);

            coDate = new DataGridViewTextBoxColumn();
           // coDate.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            coDate.DataPropertyName = "Date";
            coDate.HeaderText = "Thời Gian";
            coDate.Name = "cnameDate";
            coDate.Width = 150;
            coDate.ReadOnly = true;
            dgv.Columns.Add(coDate);

            coThread = new DataGridViewTextBoxColumn();
            coThread.DataPropertyName = "Thread";
            coThread.HeaderText = "Lượt";
            coThread.Name = "cnameThread";
            coThread.Visible = false;
            dgv.Columns.Add(coThread);

            coLevel = new DataGridViewTextBoxColumn();
            //coLevel.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            coLevel.DataPropertyName = "Level";
            coLevel.HeaderText = "Cấp độ";
            coLevel.Name = "cnameLevel";
            coLevel.Width = 80;
            coLevel.ReadOnly = true;
            dgv.Columns.Add(coLevel);

            coLogger = new DataGridViewTextBoxColumn();
            //coLogger.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            coLogger.DataPropertyName = "Logger";
            coLogger.HeaderText = "Form";
            coLogger.Name = "cnameLogger";
            coLogger.Width = 250;
            coLogger.ReadOnly = true;
            dgv.Columns.Add(coLogger);

            coMessage = new DataGridViewTextBoxColumn();
            coMessage.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            coMessage.DataPropertyName = "Message";
            coMessage.HeaderText = "Nội dung";
            coMessage.Name = "cnameMessage";
            coMessage.ReadOnly = true;
            // coID.Visible = false;
            dgv.Columns.Add(coMessage);

            LoadDS();
        }

        private void tsRefesh_Click(object sender, EventArgs e)
        {
            loadDGV();
        }

        private void sốLượngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (toolStripButton3.Visible == true)
            {
                toolStripButton3.Visible = false;
            }
            else
            {
                toolStripButton3.Visible = true;
            }
        }

        private void toolStripButton3_VisibleChanged(object sender, EventArgs e)
        {
            if (toolStripButton3.Visible == true)
            {
                toolStripButton3.Visible = true;
                toolStripButton4.Visible = true;
                toolStripTextBox1.Visible = true;
                toolStripLabel1.Visible = true;
                toolStripButton5.Visible = true;
                toolStripButton6.Visible = true;
            }
            else
            {
                toolStripButton3.Visible = false;
                toolStripButton4.Visible = false;
                toolStripTextBox1.Visible = false;
                toolStripLabel1.Visible = false;
                toolStripButton5.Visible = false;
                toolStripButton6.Visible = false;
            }
        }

        private void txtTKLogger_MouseUp(object sender, MouseEventArgs e)
        {
            txtTKLogger.Clear();
        }

        private void txtTKLogger_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTKLogger.Text))
            {
                txtTKLogger.Text = "Tìm kiếm theo mã log, nội dung log";
                XL.loadDGV(bindingNavigator1, dgv, null);
                loadDGV();
            }
        }

        private void txtTKLogger_TextChanged(object sender, EventArgs e)
        {
            XLL = new XuLyLog();
            XL.loadDGV(bindingNavigator1, dgv, XLL.SearchLog(_log, txtTKLogger.Text));
        }

        private void lbDpre_Click(object sender, EventArgs e)
        {
            PagingNumber = 0;
            loadDGV();
        }

        private void lbPre_Click(object sender, EventArgs e)
        {
            int sotranghientai = Convert.ToInt32(txtPage.Text);
            int tongtrang = Convert.ToInt32(lbPageTotal.Text);
            if (sotranghientai > 1)
            {
                PagingNumber -= PagingSize;
                loadDGV();
            }
        }

        private void lbNext_Click(object sender, EventArgs e)
        {
            int sotranghientai = Convert.ToInt32(txtPage.Text);
            int tongtrang = Convert.ToInt32(lbPageTotal.Text);
            if (sotranghientai < tongtrang)
            {
                PagingNumber += PagingSize;
                loadDGV();
            }
        }

        private void lbDnext_Click(object sender, EventArgs e)
        {
            int tongtrang = Convert.ToInt32(lbPageTotal.Text);
            PagingNumber = PagingSize * (tongtrang - 1);
            loadDGV();
        }

        private void txtPage_Leave(object sender, EventArgs e)
        {
            loadDGV();
        }

        private void txtPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bool isValid = txtPage.Text.All(char.IsDigit);
                if (isValid && txtPage.Text != "")
                {
                    int textchange = Convert.ToInt32(txtPage.Text);
                    int tongtrang = Convert.ToInt32(lbPageTotal.Text);
                    if (textchange > 0 && textchange <= tongtrang)
                    {
                        PagingNumber = PagingSize * (textchange - 1);
                        loadDGV();
                    }
                    else if (tongtrang == 1)
                    {
                        MessageBoxEx.Show("Danh sách này chỉ có 1 trang");
                    }
                    else
                    {
                        MessageBoxEx.Show("Vui lòng nhập số trang từ 1 đến " + lbPageTotal.Text);
                    }
                }
                else
                {
                    MessageBoxEx.Show("Vui lòng nhập số trang từ 1 đến " + lbPageTotal.Text);
                }
            }
        }
        private void btnXuat_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "Word Documents (*.doc)|*.doc";
            sfd.FileName = "export.doc";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                var log = db.Log_SelectAll();
                dgv.DataSource = log;
                XLL.ToCsV(dgv, sfd.FileName);
                loadDGV();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult z = DevComponents.DotNetBar.MessageBoxEx.Show("Xuất ra file word trước khi xóa log?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (z == DialogResult.Yes)
            {
                SaveFileDialog sfd = new SaveFileDialog();

                sfd.Filter = "Word Documents (*.doc)|*.doc";
                sfd.FileName = "export.doc";

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    var log = db.Log_SelectAll();
                    dgv.DataSource = log;
                    XLL.ToCsV(dgv, sfd.FileName);
                    loadDGV();
                }
                MessageBoxEx.Show("Xóa log thành công");
                a.Info(frmLogin.id+": đã xóa toàn bộ log");
                loadDGV();
            }
        }
    }
}
